package com.zja.www.service.impl;

import com.zja.www.dao.ScheduleDao;
import com.zja.www.entity.Schedule;
import com.zja.www.service.ScheduleService;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

public class ScheduleServiceImpl implements ScheduleService {
    private final ScheduleDao scheduleDao;

    public ScheduleServiceImpl(ScheduleDao scheduleDao) {
        this.scheduleDao = scheduleDao;
    }

    @Override
    public boolean createSchedule(Schedule schedule) {
        // 检查时间冲突
        if (scheduleDao.checkScheduleConflict(
                schedule.getDoctorId(),
                schedule.getDate(),
                schedule.getStartTime(),
                schedule.getEndTime())) {
            throw new IllegalStateException("该时段已有排班安排");
        }
        return scheduleDao.insert(schedule);
    }

    @Override
    public boolean updateSchedule(Schedule schedule) {
        Schedule existing = scheduleDao.getById(schedule.getId());
        if (existing == null) {
            throw new IllegalArgumentException("排班记录不存在");
        }

        // 如果修改了时间，需要检查冲突
        if (!existing.getDate().equals(schedule.getDate()) ||
                !existing.getStartTime().equals(schedule.getStartTime()) ||
                !existing.getEndTime().equals(schedule.getEndTime())) {
            if (scheduleDao.checkScheduleConflict(
                    schedule.getDoctorId(),
                    schedule.getDate(),
                    schedule.getStartTime(),
                    schedule.getEndTime())) {
                throw new IllegalStateException("新时段与其他排班冲突");
            }
        }

        return scheduleDao.update(schedule);
    }

    @Override
    public boolean deleteSchedule(int scheduleId) {
        // 检查是否有预约
        Schedule schedule = scheduleDao.getById(scheduleId);
        if (schedule != null && schedule.getBookedAppointments() > 0) {
            throw new IllegalStateException("该排班已有预约，不能删除");
        }
        return scheduleDao.delete(scheduleId);
    }

    @Override
    public Schedule getScheduleById(int scheduleId) {
        return scheduleDao.getById(scheduleId);
    }

    @Override
    public List<Schedule> getSchedulesByDate(Timestamp date) {
        return scheduleDao.getByDate(date);
    }

    @Override
    public List<Schedule> getSchedulesByDoctor(int doctorId) {
        return scheduleDao.getByDoctor(doctorId);
    }

    @Override
    public List<Schedule> getSchedulesByDepartment(int departmentId) {
        return scheduleDao.getByDepartment(departmentId);
    }

    @Override
    public boolean isScheduleAvailable(int scheduleId) {
        Schedule schedule = scheduleDao.getById(scheduleId);
        return schedule != null &&
                schedule.getBookedAppointments() < schedule.getMaxAppointments();
    }

    @Override
    public boolean checkScheduleConflict(int doctorId, Timestamp date, String startTime, String endTime) {
        return scheduleDao.checkScheduleConflict(doctorId, date, startTime, endTime);
    }

    @Override
    public boolean bookSchedule(int scheduleId) {
        Schedule schedule = scheduleDao.getById(scheduleId);
        if (schedule == null) {
            throw new IllegalArgumentException("排班不存在");
        }
        if (schedule.getBookedAppointments() >= schedule.getMaxAppointments()) {
            throw new IllegalStateException("该排班已满");
        }

        schedule.setBookedAppointments(schedule.getBookedAppointments() + 1);
        return scheduleDao.update(schedule);
    }

    @Override
    public boolean cancelScheduleBooking(int scheduleId) {
        Schedule schedule = scheduleDao.getById(scheduleId);
        if (schedule == null) {
            throw new IllegalArgumentException("排班不存在");
        }
        if (schedule.getBookedAppointments() <= 0) {
            throw new IllegalStateException("没有可取消的预约");
        }

        schedule.setBookedAppointments(schedule.getBookedAppointments() - 1);
        return scheduleDao.update(schedule);
    }

    @Override
    public List<Schedule> getAvailableSchedules(int doctorId, Timestamp date) {
        return scheduleDao.getByDoctor(doctorId).stream()
                .filter(s -> s.getDate().equals(date))
                .filter(s -> s.getBookedAppointments() < s.getMaxAppointments())
                .collect(Collectors.toList());
    }

    @Override
    public void cancelScheduleBooking(int doctorId, Timestamp time) {

    }
}
